#!/usr/bin/ruby -Ku
# -*- coding: utf-8 -*-
require 'test/unit'
require 'm3g'

class Test_Image2D < Test::Unit::TestCase
  def setup
  end

  def teadown
  end

  def test_default_variables_1
    img = M3G::Image2D.new M3G::Image2D::RGB, 64, 48

    assert_equal M3G::Image2D::RGB, img.format
    assert_equal true, img.mutable?
    assert_equal 64  , img.width
    assert_equal 48  , img.height
  end

  def test_default_variables_2

    pixels = Array.new 64*48, 255
    for y in 0..47
      for x in 0..63
        pixels[(y*64+x)*3+0] = 255-x*4   # R
        pixels[(y*64+x)*3+1] = 0         # G
        pixels[(y*64+x)*3+2] = x*4       # B
      end
    end

    img = M3G::Image2D.new M3G::Image2D::RGB, 64, 48, pixels.pack("C*")

    assert_equal M3G::Image2D::RGB, img.format
    assert_equal false, img.mutable?
    assert_equal 64   , img.width
    assert_equal 48   , img.height

    # 目で見て確認
    img.write_png "new-image-1.png"
  end

  def test_default_variables_3
    pixels  = Array.new 64*48, 255
    palette = Array.new 256*3, 255

    for y in 0..47
      for x in 0..63
        pixels[y*64+x] = x*4;
      end
    end

    for i in 0..255
      palette[i*3+0] = 255-i  # R
      palette[i*3+1] = i      # G
      palette[i*3+2] = 0      # B
    end

    img = M3G::Image2D.new M3G::Image2D::RGB, 64, 48, pixels.pack("C*"), palette.pack("C*")

    assert_equal M3G::Image2D::RGB, img.format
    assert_equal false, img.mutable?
    assert_equal 64   , img.width
    assert_equal 48   , img.height

    # 目で見て確認
    img.write_png "new-image-2.png"
  end

  def test_set
    img = M3G::Image2D.new M3G::Image2D::RGB, 64, 48

    pixels = Array.new 10*10*3, 255
    for y in 0..9
      for x in 0..9
        pixels[(y*10+x)*3+0] = x*25
        pixels[(y*10+x)*3+1] = x*25
        pixels[(y*10+x)*3+2] = 255
      end
    end
    
    # 目で見て確認
    img.write_png "set-image-before.png"

    img.pixels[10,10,10,10] = pixels.pack("C*")

    # 目で見て確認
    img.write_png "set-image-after.png"

  end

  def test_dulicate

    pixels = Array.new 64*48, 255
    for y in 0..47
      for x in 0..63
        pixels[(y*64+x)*3+0] = 255-x*4   # R
        pixels[(y*64+x)*3+1] = 0         # G
        pixels[(y*64+x)*3+2] = x*4       # B
      end
    end

    img1 = M3G::Image2D.new M3G::Image2D::RGB, 64, 48, pixels.pack("C*")

    assert_equal M3G::Image2D::RGB, img1.format
    assert_equal false, img1.mutable?
    assert_equal 64   , img1.width
    assert_equal 48   , img1.height

    img2 = img1.duplicate();

    assert_not_equal img2, img1

    # 目で見て確認    
    img2.write_png "new-image1-duplicated.png"

  end

end
